package com.realgodo.site.web.user.message;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springside.modules.orm.Page;

import com.realgodo.site.entity.space.Message;
import com.realgodo.site.entity.space.Space;
import com.realgodo.site.service.space.MessageManager;
import com.realgodo.site.service.space.SpaceManager;
import com.realgodo.site.web.user.UserDefaultController;

/**
 *站内信管理
 * 
 */
@Scope("prototype")
@Result(name = "toindex", type = "redirect", location = "${base}/user/message/")
public class IndexController extends UserDefaultController {

	private static final long serialVersionUID = 4584507339641823784L;

	@Autowired
	private MessageManager messageManager;

	@Autowired
	private SpaceManager spaceManager;

	/**
	 * 邮件列表
	 */
	private Page<Message> messagePage = new Page<Message>(PAGE_SIZE);

	/**
	 * 主题邮件列表
	 */
	private List<Message> subjectMessages;

	private Message message;

	private String title;
	private String content;

	private Long receiverId;
	private Space sender;

	public String execute() {
		this.messagePage = messageManager.findMessages(messagePage, super
				.getSpace().getId());
		super.setPageInfo(messagePage);
		return SUCCESS;
	}

	public String show() {
		message = (getId() != null) ? this.messageManager.get(getId()) : null;
		if (message != null && message.getStatus() == Message.NOT_READ) {
			message.setStatus(Message.READ);
			this.messageManager.save(message);
		}
		subjectMessages = (getId() != null) ? this.messageManager
				.findSubjectMessages(super.getSpace().getId(), getId()) : null;


		return "show";
	}

	public String reply() {
		if (getId() != null) {
			this.messageManager.reply(super.getMySpace(), title, content,
					getId());
		}
		return "toindex";
	}

	public String send() {
		if (receiverId != null) {
			sender = (Space) spaceManager.get(receiverId);
		}

		return "send";
	}

	/**
	 * 自主发站内信
	 * 
	 * @return
	 */
	public String save() {
		this.messageManager
				.send(super.getMySpace(), title, content, receiverId);
		return "toindex";
	}

	public Page<Message> getMessagePage() {
		return messagePage;
	}

	public void setMessagePage(Page<Message> messagePage) {
		this.messagePage = messagePage;
	}

	public Message getMessage() {
		return message;
	}

	public void setMessage(Message message) {
		this.message = message;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Long getReceiverId() {
		return receiverId;
	}

	public void setReceiverId(Long receiverId) {
		this.receiverId = receiverId;
	}

	public Space getSender() {
		return sender;
	}

	public List<Message> getSubjectMessages() {
		return subjectMessages;
	}

	public void setSubjectMessages(List<Message> subjectMessages) {
		this.subjectMessages = subjectMessages;
	}

}
